<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Send_sms extends CI_Controller {

	public $send_sms = false;

	function __construct() {
		parent::__construct();
		$this->load->model("users_model");
	}

	public function index() {

		//$id=0;
		if (empty($id)) {
			$id = $_GET["id"];
		}

		$ids = explode(',', $id);
		$mobile = "";
		$users = "";
		$users_name = "";

		foreach ($ids as $_flg => $_id) {

			$html = $this -> init("發簡訊", "jquery", "form");
			$this -> load -> model('client_model');
			if (!empty($_id))
				$client_query = $this -> client_model -> get_client2($_id);
			$html["client"] = $client_query -> row();

			if ($html["client"] -> mobile != "" && $html["client"] -> send_sms == "Y")
				$mobile .= "," . $html["client"] -> mobile;
			if ($html["client"] -> mobile1 != "" && $html["client"] -> mobile1 != "0" && $html["client"] -> send_sms1 == "Y")
				$mobile .= "," . $html["client"] -> mobile1;
			if ($html["client"] -> mobile2 != "" && $html["client"] -> mobile2 != "0" && $html["client"] -> send_sms2 == "Y")
				$mobile .= "," . $html["client"] -> mobile2;

			//if($id=="")
			//	$html["mobile"]=$this->session->flashdata('flashdata_to');
			//else
			//	$html["mobile"]=$mobile;

			$this -> load -> model('users_model');
			$users = $this -> users_model -> get_user_info($this -> login_lib -> users_id());
			$users_name .= $users -> name;

		}
		
		if (!empty($mobile))
			$html["mobile"] = $mobile;

		$html["users"] = $users -> name;

		$this -> load -> view('send/send_sms', $html);
		
		
	}



	function sms() {
		$html = $this -> init("簡訊日志", "jquery", "table");
		$this -> load -> model('sms_model');
		$where = "";
		$config['per_page'] = 15;
		$config['total_rows'] = $this -> sms_model -> get_sms_count($where);

		$config['url'] = '/client/sms_ajax';
		$config['num_pages'] = ceil($config['total_rows'] / $config['per_page']);
		$limit = $config['per_page'] . ",0";
		$html['query_condition'] = "";
		$html['page'] = $config;
		$html['sequence'] = "";
		$html["result"] = $this -> sms_model -> get_sms($where, $limit);
		//echo $this->db->last_query();die();
		$this -> load -> view('send/sms', $html);
	}

	function sms_ajax() {
		$this -> load -> model('sms_model');
		$where = "";
		$config['per_page'] = 15;
		$config['total_rows'] = $this -> sms_model -> get_sms_count($where);

		$config['url'] = '/client/sms_ajax';
		$config['num_pages'] = ceil($config['total_rows'] / $config['per_page']);
		$limit = $config['per_page'] . ",0";
		$html['query_condition'] = "";
		$html['page'] = $config;
		$html['sequence'] = "";
		$html["result"] = $this -> sms_model -> get_sms($where, $limit);
		//echo $this->db->last_query();die();
		$this -> load -> view('send/sms_ajax', $html);
	}

	public function send_submit() {

		$this -> load -> library('sms_lib');
		$this -> load -> library('SocketHttpRequest');
		$this -> load -> model('sms_model');
		//$SendGet = new Sms_lib();
		
		
		$data['to'] 		=  $this -> input -> post('to');
		$data['name'] 	=  $this -> input -> post('name');
		$data['content'] = $this -> input -> post('content');
		$content =         $this -> input -> post('content');
		$data['status'] = 'N';
		$return_result 	= $this -> sms_model -> add($data);

		//0988326003 ken
		//0980292123 a one
		//$msg  = mb_convert_encoding("杰哥不要！！！");
		$msg  = iconv("UTF-8", "Big-5", "杰哥不要啊！哩來來來來,叔叔請你吃棒棒糖");
		//echo $this->db->last_query();die();
		$SendGet = new SocketHttpRequest();  // 建立物件
		$shr ='http://smexpress.mitake.com.tw:9600/SmSendGet.asp?'.
								'username=80249881&password=2uuiajgi&dstaddr=0911413397'.
								'&smbody='.$msg;

		$data=array('SQL'=>$shr);						

		$this->load->model('insert_model');
		$this->insert_model->insert_to_sql($data , 'h_sql');

		$SendGet->HttpRequest(	$shr);
		
		$SendGet->sendRequest(); //發送
		echo $msg = $SendGet->getResponseBody(); //取回傳值
		

	}


	public function send_sms( $_add) {

		//$this -> load -> library('sms_lib');
		$this -> load -> library('SocketHttpRequest');
		$this -> load -> model('sms_model');
		//$SendGet = new Sms_lib();
		
		
		$data['to'] 		=  $this -> input -> post('to');
		$data['name'] 	=  $this -> input -> post('name');
		$data['content'] = $this -> input -> post('content');
		$content =         $this -> input -> post('content');
		$data['status'] = 'N';
		$return_result 	= $this -> sms_model -> add($data);

		//0988326003 ken
		//0980292123 a one
		//$msg  = mb_convert_encoding("杰哥不要！！！");
		$msg  = iconv("UTF-8", "Big-5", "杰哥不要啊！哩來來來來,叔叔請你吃棒棒糖");
		//echo $this->db->last_query();die();

		

		$_many  = 'http://smexpress.mitake.com.tw:9600/SmSendPost.asp?'.
				  'username=80249881&password=2uuiajgi&encoding=Big5'.$_add;

		$_along = 'http://smexpress.mitake.com.tw:9600/SmSendGet.asp?'.
				  'username=80249881&password=2uuiajgi&encoding=Big5'.$_add;
		//'http://smexpress.mitake.com.tw:7002/SpLmGet?username=使用者帳號&password=使用者密碼'		  
		$_long_along = 'http://smexpress.mitake.com.tw:7002/SpLmGet?'.
				  'username=80249881&password=2uuiajgi&encoding=Big5'.$_add;

		$_long_many = 'http://smexpress.mitake.com.tw:7002/SpLmPost?'.
				  'username=80249881&password=2uuiajgi&encoding=Big5'.$_add;		  		  

		
		//$sql_str

		$data=array('SQL'=>$_along);							
		$this->load->model('insert_model');
		$this->insert_model->insert_to_sql($data , 'h_sql');


		$SendGet = new SocketHttpRequest();  // 建立物件
		$SendGet->HttpRequest(	$_along);
		
		$SendGet->sendRequest(); //發送
		echo $msg = $SendGet->getResponseBody(); //取回傳值
		


	}

	function do_send_sms() {


		
		$demand_id = $_POST["demand_id"]?$_POST["demand_id"]:'';
		$content = $_POST["content"]?$_POST["content"]:'';
		$market_ids = $_POST["market_ids"]?$_POST["market_ids"]:'';
		$object_ids = $_POST["object_ids"]?$_POST["object_ids"]:'';

		$markets = explode(',', $market_ids);
		$objects = explode(',', $object_ids);
		

		//if(!empty($objects))
		$objects_str='';
		foreach($objects as $k => $id){
			
			$user = $this->users_model->get_user($id);
			//echo $user[0]->phone.','. $user[0]->name;
			$objects_str.=
							'&DestName='.$user[0]->name.
							'&dstaddr='.$user[0]->phone.						  	
						  	'&smbody='.$content;
			
		}

		echo $_many  =  'http://smexpress.mitake.com.tw:9600/SmSendPost.asp?'.
				  		'username=80249881&password=2uuiajgi&encoding=utf8'.$objects_str;
		//$this->send_sms( $objects_str );			
		//$this->send_sms($phone, $name, $content ,$_to ); 

	}

	public function send_submit_old() {
		$this -> load -> model('sms_model');
		$data['to'] = $this -> input -> post('to');
		$data['name'] = $this -> input -> post('name');
		$data['content'] = $this -> input -> post('content');
		$data['status'] = 'N';
		$return_result = $this -> sms_model -> add($data);

		//echo $this->db->last_query();die();

		if ($return_result > 0) {
			$to = explode(",", $data['to']);
			$i = 0;
			foreach ($to as $key => $val) {
				$h = str_replace("-", "", $val);

				$this -> sms_lib -> HttpRequest('http://smexpress.mitake.com.tw:9600/SmSendGet.asp?username=12956227&password=michael0935&dstaddr=' . 
				$h . '&DestName=' . $data['name'] . '&smbody=' . iconv("UTF-8", "Big-5", $data['content']));
				//echo 'http://smexpress.mitake.com.tw:9600/SmSendGet.asp?username=12956227&password=michael0935&dstaddr='.$h.'&DestName='.$data['name'].'&smbody='.iconv("UTF-8","Big-5",$data['content']);
				if ($this -> sms_lib -> sendRequest()) {
					$i++;
				}
				//echo iconv("Big-5","UTF-8",$this->sms_lib->getResponseBody()); //取回傳值
			}

			if ($i == count($to)) {

				$data['id'] = $return_result;
				$data['status'] = "Y";
				$this -> sms_model -> update_status($data);
			}

			redirect("/send/sms");
		} else {
			echo "插入失敗";
		}

	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
